﻿using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Services.Utils;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace FPSO.Services.Impl
{
	//所有和舱室任务相关的数据库接口服务
    public class DBTankTaskService : DbRepository<TankTask>, ITankTaskService
    {
        public void AddTask(TankTask task)
        {
            task.WBTDetail = JsonConvert.SerializeObject(task.WBTTaskList);
            InsertEntity(task);
        }

        public void DeleteTask(int taskId)
        {
            DeleteById(taskId);
        }
		public void UpdateTask(TankTask task)
		{
			TankTask taskExist = GetTask(task.TaskId);
			if (taskExist == null)
				AddTask(task);
			else
			{
				task.WBTDetail = JsonConvert.SerializeObject(task.WBTTaskList);
				UpdateEntity(task);
			}
		}

		public TankTask GetTask(int taskId)
        {
            TankTask task = QueryEntity(t => t.TaskId == taskId);
            if (task != null)
            {
                task.WBTTaskList = JsonConvert.DeserializeObject<List<WBTTask>>(task.WBTDetail);
            }
            return task;
        }
		//通过taskId在task中找pretaskid，相同的即为后续步骤
		public TankTask GetFollowTask(int taskId)
		{
			TankTask task = QueryEntity(t => t.PreTaskId == taskId);
			if (task != null)
			{
				task.WBTTaskList = JsonConvert.DeserializeObject<List<WBTTask>>(task.WBTDetail);
			}
			return task;
		}

		/// 根据某TaskId在WaitAndChecking的Task中找到所有相关的任务，即通过TaskId在task中找pretaskid，相同的即为后续步骤
		public List<TankTask> GetWaitAndCheckingTaskListByTaskId(int taskId)
		{
			if(TankConstant.g_Onboard)
				return null;

			List<TankTask> resList = new List<TankTask>();
			TankTask oriTask = GetTask(taskId);
			if (oriTask != null)
			{
				TankTask followTask = GetFollowTask(taskId);
				while (followTask != null)
				{
					resList.Add(followTask);
					taskId = followTask.TaskId;
					followTask = GetFollowTask(taskId);
				}
			}
			return resList;
		}
		public List<TankTask> GetWaitAndCheckingTaskList(int taskQueueId)
		{
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format("task_queue = {0} AND (task_state = {1} OR task_state = {2}) AND plan_end_time > '{3}'",
				taskQueueId, TaskStateConstant.WATING_ID, TaskStateConstant.CHECK_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
			DeserializeWBTTaskList(taskList);
			return taskList;
		}

		public List<TankTask> GetWaitAndExecingTaskList(int taskQueueId)
        {
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format("task_queue = {0} AND (task_state = {1} OR task_state = {2}) AND plan_end_time > '{3}'",
                taskQueueId, TaskStateConstant.WATING_ID, TaskStateConstant.EXEC_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
            DeserializeWBTTaskList(taskList);
            return taskList;
        }

        public List<TankTask> GetWaitAndExecingCheckingTaskList(int taskQueueId)
        {
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format("task_queue = {0} AND (task_state = {1} OR task_state = {2} OR task_state = {3}) AND plan_end_time > '{4}'",
            taskQueueId, TaskStateConstant.WATING_ID, TaskStateConstant.EXEC_ID, TaskStateConstant.CHECK_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
            DeserializeWBTTaskList(taskList);
            return taskList;
        }

		public List<TankTask> GetAllWaitTaskList()
		{
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format(" task_state = {0} AND plan_end_time > '{1}'",
				TaskStateConstant.WATING_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
			DeserializeWBTTaskList(taskList);
			return taskList;
		}

		public List<TankTask> GetAllExecingTaskList()
		{
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format(" task_state = {0} AND plan_end_time > '{1}'",
				TaskStateConstant.EXEC_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
			DeserializeWBTTaskList(taskList);
			return taskList;
		}
		public List<TankTask> GetAllWaitAndExecingTaskList()
        {
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format(" (task_state = {0} OR task_state = {1}) AND plan_end_time > '{2}'",
                TaskStateConstant.WATING_ID, TaskStateConstant.EXEC_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
            DeserializeWBTTaskList(taskList);
            return taskList;
        }

		//查找所有生成通过校核、等待执行、正在执行的任务
        public List<TankTask> GetAllWaitAndExecingAndCheckingTaskList()
        {
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format(" (task_state = {0} OR task_state = {1} OR task_state = {2}) AND plan_end_time > '{3}'",
                TaskStateConstant.WATING_ID, TaskStateConstant.EXEC_ID, TaskStateConstant.CHECK_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
            DeserializeWBTTaskList(taskList);
            return taskList;
        }

		//查找所有生成通过校核、等待执行的任务
		public List<TankTask> GetAllWaitingAndCheckingTaskList()
        {
			if (TankConstant.g_Onboard)
				return null;

			string condition = string.Format(" (task_state = {0} OR task_state = {1}) AND plan_end_time > '{2}'",
                TaskStateConstant.WATING_ID, TaskStateConstant.CHECK_ID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<TankTask> taskList = QueryListByCondition(condition, "plan_start_time");
            DeserializeWBTTaskList(taskList);
            return taskList;
        }

        public bool IsTaskUpdate(int queueId, DateTime reflushTime)
        {
            return QueryCount(t => t.TaskQueueId == queueId && t.UpdateTime >= reflushTime) > 0;
        }

        public List<TankTask> GetExecingTaskList(int taskQueueId)
        {
			if (TankConstant.g_Onboard)
				return null;

			List<TankTask> taskList = QueryListByCondition(t => t.TaskQueueId == taskQueueId && t.TaskState == TaskStateConstant.EXEC_ID);
            DeserializeWBTTaskList(taskList);
            return taskList;
        }


        /// <summary>
        /// 获取等待执行的任务列表
        /// </summary>
        /// <param name="taskQueueId"></param>
        /// <returns></returns>
        public List<TankTask> GetWaitingTaskList(int taskQueueId)
        {
			if (TankConstant.g_Onboard)
				return null;

			List<TankTask> taskList = QueryListByCondition(t => t.TaskQueueId == taskQueueId && t.TaskState == TaskStateConstant.WATING_ID);
            DeserializeWBTTaskList(taskList);
            return taskList;
        }

        private void DeserializeWBTTaskList(List<TankTask> taskList)
        {
            foreach(TankTask task in taskList)
            {
                task.WBTTaskList = JsonConvert.DeserializeObject<List<WBTTask>>(task.WBTDetail);
            }    
        }
    }
}
